#include "qyklabel.h"
#include <QPainter>
#include "YK16GrayImage.h"

using namespace std;

qyklabel::qyklabel(QWidget *parent)
	: QLabel(parent)
{
	m_pYK16Image = NULL;
	m_pYK16ImageROI = NULL;
	m_pCurSnake = NULL;
	//this->width();
	//m_Rt = this->rect();
	//m_bDirection = false; // = hor

	//m_Rt.setRect()
	m_bDrawPoints = true;

	//m_rtDspROIRect = QRect(0,0,0,0);

	connect(this, SIGNAL(Mouse_Pressed()), this, SLOT(ReDraw()));

}

qyklabel::~qyklabel()
{
}

void qyklabel::SetLineDirection(bool bDirection)
{
	//m_bDirection = bDirection;
	update();
}

void qyklabel::mouseMoveEvent( QMouseEvent *ev )
{
	//this->x	= ev->x();
	//this->y = ev->y();
	//emit Mouse_Pos();
}

void qyklabel::mousePressEvent( QMouseEvent *ev )
{
	this->x	= ev->x();
	this->y = ev->y();
	emit Mouse_Pressed();
}

void qyklabel::mouseReleaseEvent( QMouseEvent *ev )
{
	this->x	= ev->x();
	this->y = ev->y();
	emit Mouse_Released();
}


void qyklabel::mouseDoubleClickEvent( QMouseEvent *ev )
{
	this->x	= ev->x();
	this->y = ev->y();
	//emit Mouse_DoubleClicked();
}

void qyklabel::leaveEvent( QEvent * )
{
	//emit Mouse_Left();
}

void qyklabel::paintEvent( QPaintEvent * )
{
	QPainter painter(this);
	
	painter.setPen( QPen(Qt::black, 2));
	QRect TargetRt = rect();	
	painter.drawRect(TargetRt);

	if (m_pYK16Image != NULL)
	{		
		painter.drawImage(TargetRt, m_pYK16Image->m_QImage); //it Works!
	}
	
	//DPGMTracking* pCurApp = (DPGMTracking*)qApp;
	//pCurApp->ui.lineEditPixVal->setText("abcde");

	//if (m_pYK16ImageROI != NULL && !m_rtDspROIRect.isEmpty() && m_bROIDisp)	
	//{		
	//	if (!m_pYK16ImageROI->m_QImage.isNull() && m_rtDspROIRect.width()> 5 && m_rtDspROIRect.height()> 5)
	//		painter.drawImage(m_rtDspROIRect, m_pYK16ImageROI->m_QImage); //it Works!		
	//}
	//if(m_pCurSnake != NULL)
	//{
	//	//int cnt = m_pCurSnake->m_log.Sm;
	//	int cnt = m_pCurSnake->m_log.Sm;
	//	int arrSize = cnt - m_pCurSnake->m_vExcludedIdx.size();


	//	if (cnt > 0)
	//	{
	//		painter.setPen( QPen(Qt::green, 3));
	//		QPoint* viewSnakePt = new QPoint [arrSize];
	//		int j = 0;

	//		for (int i = 0; i <cnt ; i++)
	//		{
	//			vector<int>::iterator it;
	//			
	//			bool bExistInList = false;
	//			
	//			for (it= m_pCurSnake->m_vExcludedIdx.begin() ; it != m_pCurSnake->m_vExcludedIdx.end() ; it++)
	//			{
	//				if (i == (*it))
	//				{
	//					bExistInList =true;
	//					break;
	//				}
	//			}

	//			if(!bExistInList)
	//			{
	//				QPointF ptInROI = QPointF(m_pCurSnake->m_Seed[i].point.x, m_pCurSnake->m_Seed[i].point.y);
	//				QPointF ptInWholeImg = QPointF(ptInROI.x()+m_pCurSnake->m_ptOffset.x(), ptInROI.y()+m_pCurSnake->m_ptOffset.y());
	//				viewSnakePt[j] = Data2View(ptInWholeImg, this->width(), this->height(),m_pYK16Image->m_iWidth,
	//					m_pYK16Image->m_iHeight);
	//				j++;
	//			}
	//		}
	//		painter.drawPoints(viewSnakePt, arrSize);
	//		delete [] viewSnakePt;
	//	}		
	//}

	/*int top = m_rtDspROIRect.top();
	int left = m_rtDspROIRect.left();
	int right = m_rtDspROIRect.right();
	int bottom = m_rtDspROIRect.bottom();*/

	/*if (m_rtDspROIRect.top() >=0
		&& m_rtDspROIRect.left() >= 0
		&& m_rtDspROIRect.right() >0
		&& m_rtDspROIRect.bottom() >0)
	{
		painter.setPen( QPen(Qt::green, 2));
		painter.drawRect(m_rtDspROIRect);
	}	*/

	//Draw Snake points
	//class should be transmitted here

	//if (x> 0  && y>0)
	//{
	//	painter.setPen(QPen(Qt::red, 1, Qt::DotLine));
	//	if (m_bDirection == false)
	//	{			
	//	//	painter.drawLine(QPoint(0,y),QPoint(width()-1, y));
	//	}

	//	if (m_bDirection == true)
	//	{
	//	//	painter.drawLine(QPoint(x,0),QPoint(x, height()-1));
	//	}	
	//}
}

void qyklabel::SetBaseImage( YK16GrayImage* pYKImage )
{
	if (pYKImage->m_pData != NULL && !pYKImage->m_QImage.isNull())
		m_pYK16Image = pYKImage;
}

void qyklabel::ConvertAndCopyPoints(vector<QPoint>& vSrcPoint, int iDataWidth, int iDataHeight)
{
	m_vPt.clear();	

	int dspWidth = this->width();
	int dspHeight = this->height();


	vector<QPoint>::iterator it;

	for (it = vSrcPoint.begin() ; it != vSrcPoint.end() ; it++)
	{
		///*if ((*it).x() == 381 && (*it).y() > 1000)
		//	int test = 5;

		//if ((*it).x() == 581 && (*it).y() > 1000)
		//	int test = 5;*/


		QPoint tmpDspPt = Data2View((*it),dspWidth, dspHeight, iDataWidth, iDataHeight);
		m_vPt.push_back(tmpDspPt);
	}
}


QPoint qyklabel::View2Data(QPoint viewPt, int viewWidth, int viewHeight, int dataWidth, int dataHeight)
{
	double fZoomX = viewWidth / (double)dataWidth;
	double fZoomY = viewHeight / (double)dataHeight;

	QPoint dataPt;
	dataPt.setX(qRound(viewPt.x() / fZoomX));
	dataPt.setY(qRound(viewPt.y() / fZoomY));

	return dataPt;
}

QPoint qyklabel::Data2View(QPointF dataPt, int viewWidth, int viewHeight, int dataWidth, int dataHeight)
{
	double fZoomX = viewWidth / (double)dataWidth;
	double fZoomY = viewHeight / (double)dataHeight;

	QPoint viewPt;
	viewPt.setX(qRound(dataPt.x() * fZoomX));
	viewPt.setY(qRound(dataPt.y() * fZoomY));

	return viewPt;

}

void qyklabel::SetDrawPointToggle(bool bToggle )
{
	if (bToggle)
		m_bDrawPoints = true;
	else
		m_bDrawPoints = false;


	update();
}

void qyklabel::ReDraw()
{
	update();
}
//
//void qyklabel::SetSnake( CSnake* pCurSnake )
//{
//	if (pCurSnake == NULL)
//		return;
//
//	m_pCurSnake = pCurSnake;
//}
//
//
//void qyklabel::setDspROIRect(QRect& dspRt)
//{
//	m_rtDspROIRect = dspRt;
//}
//
//void qyklabel::SetROIImage( YK16GrayImage* pYKImage )
//{
//	m_pYK16ImageROI = pYKImage;
//}
//
//void qyklabel::setROIDspSwitch( bool bChecked )
//{
//	m_bROIDisp = bChecked;
//}